package org.jeecg.modules.santu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.santu.entity.TbGroup;
import org.jeecg.modules.santu.service.ITbGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 群组表
* @Author: jeecg-boot
* @Date:   2024-11-14
* @Version: V1.0
*/
@Api(tags="群组表")
@RestController
@RequestMapping("/santu/tbGroup")
@Slf4j
public class TbGroupController extends JeecgController<TbGroup, ITbGroupService> {
   @Autowired
   private ITbGroupService tbGroupService;

   /**
    * 分页列表查询
    *
    * @param tbGroup
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "群组表-分页列表查询")
   @ApiOperation(value="群组表-分页列表查询", notes="群组表-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(TbGroup tbGroup,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<TbGroup> queryWrapper = QueryGenerator.initQueryWrapper(tbGroup, req.getParameterMap());
       Page<TbGroup> page = new Page<TbGroup>(pageNo, pageSize);
       IPage<TbGroup> pageList = tbGroupService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

    @GetMapping(value = "/getDefaultCheckedStatus")
    public Result<?> getDefaultCheckedStatus() {
        List<TbGroup> list = tbGroupService.list();
        boolean mark = false;
        for (int i = 0; i < list.size(); i++) {
            TbGroup tbGroup = list.get(i);
            String roomid = tbGroup.getRoomid();
            if(roomid.equals("44834132021@chatroom")){
                Integer enable = tbGroup.getEnable();
                if(enable==TbGroup.ENABLE){
                    mark = true;
                }else{
                    mark = false;
                }
                break;
            }
        }
        return Result.OK(mark);
    }

    @GetMapping(value = "/onEnableTest")
    public Result<?> onEnableTest() {
        List<TbGroup> list = tbGroupService.list();
        for (int i = 0; i < list.size(); i++) {
            TbGroup tbGroup = list.get(i);
            String roomid = tbGroup.getRoomid();
            if (roomid.equals("44834132021@chatroom")) {
                tbGroup.setEnable(TbGroup.ENABLE);
                boolean b = tbGroupService.updateById(tbGroup);
                System.out.println("开启是否成功" + b);
            } else {
                tbGroup.setEnable(TbGroup.FORBIDDEN);
                boolean b = tbGroupService.updateById(tbGroup);
                System.out.println("禁用是否成功" + b);
            }
        }
        return Result.OK("success");
    }

    @GetMapping(value = "/notEnableTest")
    public Result<?> notEnableTest() {
        List<TbGroup> list = tbGroupService.list();
        for (int i = 0; i < list.size(); i++) {
            TbGroup tbGroup = list.get(i);
            String roomid = tbGroup.getRoomid();
            if (roomid.equals("44834132021@chatroom")) {
                tbGroup.setEnable(TbGroup.FORBIDDEN);
                boolean b = tbGroupService.updateById(tbGroup);
                System.out.println("开启是否成功" + b);
            } else {
                tbGroup.setEnable(TbGroup.ENABLE);
                boolean b = tbGroupService.updateById(tbGroup);
                System.out.println("禁用是否成功" + b);
            }
        }
        return Result.OK("success");
    }
   /**
    *   添加
    *
    * @param tbGroup
    * @return
    */
   @AutoLog(value = "群组表-添加")
   @ApiOperation(value="群组表-添加", notes="群组表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody TbGroup tbGroup) {
       tbGroupService.save(tbGroup);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param tbGroup
    * @return
    */
   @AutoLog(value = "群组表-编辑")
   @ApiOperation(value="群组表-编辑", notes="群组表-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody TbGroup tbGroup) {
       tbGroupService.updateById(tbGroup);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "群组表-通过id删除")
   @ApiOperation(value="群组表-通过id删除", notes="群组表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       tbGroupService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "群组表-批量删除")
   @ApiOperation(value="群组表-批量删除", notes="群组表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.tbGroupService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "群组表-通过id查询")
   @ApiOperation(value="群组表-通过id查询", notes="群组表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       TbGroup tbGroup = tbGroupService.getById(id);
       if(tbGroup==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(tbGroup);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param tbGroup
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, TbGroup tbGroup) {
       return super.exportXls(request, tbGroup, TbGroup.class, "群组表");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, TbGroup.class);
   }

}
